//
//  JsInputViewAlpha.m
//  CanvasAlpha
//
//  Created by Gikki Ares on 2021/4/7.
//  Copyright © 2021 vgemv. All rights reserved.
//

#import "JsInputView.h"
#import "JsGeometryManager.h"
@interface JsInputViewVm ()

@end

@implementation JsInputViewVm

- (UIImage *)logoImage {
	if(!_logoImage) {
		_logoImage = [UIImage imageNamed:@"ugc.png"];
	}
	return _logoImage;
}


@end



@interface JsInputView ()
{
	UILabel * mlb_title;
	UIImageView * miv_logo;
	UILabel * mlb_message;
	UITextView * mtv;
	UIButton * mbtn_cancel;
	UIButton * mbtn_confirm;
	UIView * mv_separatorH;
	UIView * mv_separatorV;
	
}

@property(nonatomic,strong)JsInputViewVm * mvm;

@end


//ui样式,存放一些固定的基本配置;
//vm中,存放可变的配置.可以替换.
//显示,都是先得到model
//然后根据model来进行显示.
@interface JsInputView ()
{
	float mf_titleHeight;
	float mf_messageHeight;
	float mf_logoSize;
	float mf_spcaing;
	float mf_spcaingH;
	float mf_textViewHeight;
	float mf_btnHeight;
	
	UIFont * mFont_title;
	UIFont * mFont_message;
	UIFont * mFont_content;
}



@end



@implementation JsInputView

+ (JsInputView *)newInstanceWithModel:(JsInputViewVm *)vm {
	//高度之后再计算得出.
	JsInputView * view = [[JsInputView alloc]initWithFrame:CGRectMake(0, 0, 300, 0)];
	view.mvm = vm;
	return view;
}

- (instancetype)initWithFrame:(CGRect)frame {
	if(self == [super initWithFrame:frame]) {
		[self commonInit];
	}
	return self;
}

- (instancetype)init {
	if(self == [super init]) {
		[self commonInit];
		
	}
	return self;
}

- (void)commonInit {
	mFont_title = [UIFont boldSystemFontOfSize:20];
	mFont_message = [UIFont systemFontOfSize:15];
	mFont_content = [UIFont systemFontOfSize:15];
	
	mf_spcaing = 10;
	mf_spcaingH = 20;
	mf_logoSize = 80;
	mf_btnHeight = 50;
}


- (void)setMvm:(JsInputViewVm *)mvm {
	_mvm = mvm;
	[self setNeedsLayout];
}


- (JsInputViewVm *)vm {
	return _mvm;
}

- (void)shake {
	CGFloat t =4.0;
	CGAffineTransform translateRight  =CGAffineTransformTranslate(CGAffineTransformIdentity, t,0.0);
	CGAffineTransform translateLeft =CGAffineTransformTranslate(CGAffineTransformIdentity,-t,0.0);
	self.transform = translateLeft;
	[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
		[UIView setAnimationRepeatCount:2.0];
		self.transform = translateRight;
	} completion:^(BOOL finished){
		if(finished){
			[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
				self.transform =CGAffineTransformIdentity;
			} completion:NULL];
		}
	}];
}

//自身的宽高,由自己的vm来决定.
+ (JsInputView *)newInstance {
	//height  =
	return [[JsInputView alloc]initWithFrame:CGRectMake(0, 0, 300, 500)];
}


- (void)setupUi:(JsInputViewVm *)vm {
	self.backgroundColor = [UIColor whiteColor];
	self.layer.cornerRadius = 5;
	self.layer.masksToBounds = YES;
	{
		//title
		if(!mlb_title) {
			mlb_title = [UILabel new];
			[self addSubview:mlb_title];
			mlb_title.font = mFont_title;
			mlb_title. textColor = [UIColor blackColor];
		}
		mlb_title.text = vm.title;
		CGSize size = [JsGeometryManager sizeForOneLineStringWithContext:vm.title font:mFont_title];
		mlb_title.frame = [JsGeometryManager frameHAlignCenterVAlignTopWithContainerSize:self.bounds.size size:size offset:CGPointMake(0, mf_spcaing)];
	}
	{
			//logo
		if(!miv_logo) {
			miv_logo = [UIImageView new];
			[self addSubview:miv_logo];
			
		}
		miv_logo.layer.cornerRadius = mf_logoSize/2;
		miv_logo.layer.masksToBounds = YES;
		miv_logo.frame = [JsGeometryManager frameHCenterVBelowWithContainerSize:self.bounds.size size:self.bounds.size siblingFrame:mlb_title.frame];
		miv_logo.image = [UIImage imageNamed:@"ugc.png"];
	}
	{
			//message
		if(!mlb_message) {
			mlb_message = [UILabel new];
			mlb_message.font = mFont_message;
			mlb_message. textColor = [UIColor blackColor];
			[self addSubview:mlb_message];
			mlb_message.numberOfLines = 0;
		}
		int w = self.bounds.size.width - mf_spcaingH * 2;
		mlb_message.text = vm.message;
		CGSize size = [JsGeometryManager sizeForMultiLineStringWithContext:vm.message font:mFont_message maxWidth:w isWidthFixed:YES maxHeight:0 minHeight:10];
		mlb_message.frame = [JsGeometryManager frameHCenterVBelowWithContainerSize:self.bounds.size size:size siblingFrame:miv_logo.frame offset:CGPointMake(0,mf_spcaing)];
		
	}
	

	
	
	{
			//tv
		if(!mtv) {
			mtv = [UITextView new];
			[self addSubview:mtv];
			mtv.layer.cornerRadius = 3;
			mtv.layer.borderColor = [UIColor lightGrayColor].CGColor;
			mtv.layer.borderWidth = 1;
		}
		mtv.text = vm.context;
		mtv.font = mFont_content;
		int w = self.bounds.size.width - mf_spcaingH * 2;
		CGSize size = [JsGeometryManager sizeForMultiLineStringWithContext:vm.context font:mFont_content maxWidth:w isWidthFixed:YES maxHeight:0 minHeight:120];
		mtv.frame = [JsGeometryManager frameHCenterVBelowWithContainerSize:self.bounds.size size:size siblingFrame:mlb_message.frame offset:CGPointMake(0, mf_spcaing)];
	}
	
	
			{
				if(!mv_separatorH) {
					mv_separatorH = [UIView new];
					[self addSubview:mv_separatorH];
					mv_separatorH.backgroundColor = [UIColor lightGrayColor];
				}
				int y =  [JsGeometryManager yBelowFrame:mtv.frame offset:mf_spcaing*2];
				mv_separatorH.frame = CGRectMake(0, y, self.bounds.size.width, 1);
			}
	
	{
		if(!mv_separatorV) {
			mv_separatorV = [UIView new];
			[self addSubview:mv_separatorV];
			mv_separatorV.backgroundColor = [UIColor lightGrayColor];
		}
		int y =  [JsGeometryManager yBelowFrame:mtv.frame offset:mf_spcaing*2];
		int x =  self.bounds.size.width/2;
		int w = 1;
		int h = mf_btnHeight;
		mv_separatorV.frame = CGRectMake(x, y, w, h);
	}
	
	
	{
			//cancel btn
		if(!mbtn_cancel) {
			mbtn_cancel = [UIButton new];
			[self addSubview:mbtn_cancel];
			mbtn_cancel.tag = 0;
			[mbtn_cancel setTitle:@"取消" forState:UIControlStateNormal];
			[mbtn_cancel addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
			[mbtn_cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
		}
		int x = 0;
		int y =  [JsGeometryManager yBelowFrame:mtv.frame offset:mf_spcaing*2];
		int w = self.bounds.size.width/2;
		int h = mf_btnHeight;
		mbtn_cancel.frame = CGRectMake(x, y, w, h);
	}
	{
			//confirm btn
		if(!mbtn_confirm) {
			mbtn_confirm = [UIButton new];
			[self addSubview:mbtn_confirm];
			mbtn_confirm.tag = 1;
			[mbtn_confirm addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
			[mbtn_confirm setTitle:@"确定" forState:UIControlStateNormal];
			[mbtn_confirm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
		}
		int x = self.bounds.size.width/2;;
		int y =  [JsGeometryManager yBelowFrame:mtv.frame offset:mf_spcaing*2];
		int w = self.bounds.size.width/2;
		int h = mf_btnHeight;
		mbtn_confirm.frame = CGRectMake(x, y, w, h);
	}
	
	int height = [JsGeometryManager yBelowFrame:mbtn_confirm.frame offset:0];
	self.bounds = CGRectMake(0, 0, 300,height);
	
}

- (void)layoutSubviews {
	[super layoutSubviews];
	[self setupUi:_mvm];
}

- (void)onClick:(UIButton *)btn {
	_mvm.context = mtv.text;
	if(btn.tag == 1) {
		//ok
		if(mtv.text.length == 0) {
			[self shake];
			return;
		}
	}
	
	if(_delegate&&[_delegate respondsToSelector:@selector(jsInputView:clickIndex:)]) {
		[_delegate jsInputView:self clickIndex:(int)btn.tag];
	}
}

@end
